package com.hhq.demo.controller;

import com.hhq.demo.bean.Courier;
import com.hhq.demo.bean.CourierList;
import com.hhq.demo.bean.ResultData;
import com.hhq.demo.mvc.ResponseBody;
import com.hhq.demo.service.CourierService;
import com.hhq.demo.service.impl.CourierServiceImpl;
import com.hhq.demo.utils.JSONUtil;
import com.hhq.demo.utils.Message;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author HHQ
 * @date 2021年12月16日
 */
public class CourierController {

    CourierService courierService = new CourierServiceImpl();

    @ResponseBody("/courier/consoleData.do")
    public String consoleData(HttpServletRequest request, HttpServletResponse response){
        Message message = new Message();
        Map<String, Object> map = courierService.consoleData();
        if (map.isEmpty()) {
            message.setStatus(-1);
        } else {
            message.setStatus(0);
        }
        message.setData(map);
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/courier/listLimit.do")
    public String listLimit(HttpServletRequest request, HttpServletResponse response){
        Integer offset = Integer.parseInt(request.getParameter("offset"));
        Integer pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<CourierList> courierList = courierService.listLimit(offset, pageNumber);
        ResultData<CourierList> data = new ResultData<>();
        data.setTotal((Integer) courierService.consoleData().get("totalCourier"));
        data.setRows(courierList);
        return JSONUtil.toJSON(data);
    }

    @ResponseBody("/courier/deleteById.do")
    public String deleteById(HttpServletRequest request, HttpServletResponse response){
        Integer id = Integer.parseInt(request.getParameter("id"));
        int flag = courierService.deleteById(id);
        Message message = new Message();
        if (flag==1) {
            message.setStatus(1);
            message.setResult("删除成功！");
        } else {
            message.setStatus(0);
            message.setResult("删除失败，请稍后再试！");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/courier/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String idNumber = request.getParameter("idNumber");
        String phone = request.getParameter("phone");
        Courier courier = new Courier();
        courier.setUsername(username);
        courier.setPassword(password);
        courier.setPhone(phone);
        courier.setIdNumber(idNumber);
        int flag = courierService.insert(courier);
        Message msg = new Message();
        if(flag==1){
            msg.setStatus(1);
            msg.setResult("添加成功!");
        }else{
            msg.setStatus(0);
            msg.setResult("添加失败!");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/courier/findByPhone.do")
    public String findByPhone(HttpServletRequest request, HttpServletResponse response){
        String phone = request.getParameter("phone");
        Courier byPhone = courierService.findByPhone(phone);
        Message message = new Message();
        if (byPhone.getId()!=null) {
            message.setStatus(1);
            message.setResult("查找成功");
            message.setData(byPhone);
        } else {
            message.setStatus(0);
            message.setResult("查找失败，请检查手机号码是否正确");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String idNumber = request.getParameter("idNumber");
        String oldPhone = request.getParameter("oldPhone");
        Integer id = Integer.parseInt(request.getParameter("id"));
        Courier courier = new Courier();
        courier.setUsername(username);
        courier.setPassword(password);
        courier.setIdNumber(idNumber);
        courier.setPhone(phone);
        int flag = courierService.update(id, oldPhone, courier);
        if (flag==1) {
            return JSONUtil.toJSON(new Message(1,"修改成功"));
        } else {
            return JSONUtil.toJSON(new Message(0,"修改失败"));
        }
    }
}
